package io.gitee.chearnee.fw.datasecurity.core.common;

import io.gitee.chearnee.fw.datasecurity.core.handler.desensitization.SDRuleHandler;
import io.gitee.chearnee.fw.datasecurity.core.handler.desensitization.SDRuleHandlerFactory;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author cn
 * @version 1.0
 * @description
 * @date 11/21/2022 4:14 PM
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class SDAnnotationCacheUtil {

    //脱敏注解缓存
    private static final List<Class<?>> cacheSDAnnotations = new ArrayList<>();
    //脱敏注解的name
    private static Set<String> cacheSDAnnotationNames;

    private static void init() {
        if (cacheSDAnnotations.isEmpty()) {
            synchronized (cacheSDAnnotations) {
                if (cacheSDAnnotations.isEmpty()) {
                    Map<Class<? extends Annotation>, SDRuleHandler<? extends Annotation>> handlerMap = SDRuleHandlerFactory.getHandlerMap();
                    Set<Class<? extends Annotation>> classes = handlerMap.keySet();
                    cacheSDAnnotations.addAll(classes);
                    cacheSDAnnotationNames = cacheSDAnnotations.stream().map(Class::getName).collect(Collectors.toSet());
                }
            }
        }
    }

    public static List<Class<?>> getCacheSDAnnotations() {
        init();
        return cacheSDAnnotations;
    }

    public static Set<String> getCacheSDAnnotationNames() {
        init();
        return cacheSDAnnotationNames;
    }
}
